package com.yunhe.abnormal.controller.base;

import com.yunhe.abnormal.domain.base.AbnormalRule;
import com.yunhe.abnormal.dto.AbnormalRuleDTO;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.exception.ObjectNotFoundException;
import com.yunhe.common.exception.UniqueConstraintsException;
import com.yunhe.common.model.PageParam;
import com.yunhe.common.model.ResultObject;
import com.yunhe.abnormal.service.base.AbnormalRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import zju.fes.exp.IllegalExpressionException;
import zju.fes.exp.IncalculableExpressionException;
import zju.fes.exp.UnknownOperatorException;

import java.util.List;
import java.util.Map;
import java.util.Optional;

@Api(value = "abnormalRule", description = "异常规则维护", tags = "Base_AbnormalRule")
@RestController
@RequestMapping("/abnormalRules")
public class AbnormalRuleController {

    @Autowired
    private AbnormalRuleService abnormalRuleService;

    @ApiOperation("新增异常规则")
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<ResultObject> create(@RequestBody AbnormalRule abnormalRule) throws ObjectNotFoundException, UniqueConstraintsException,
            ArgumentErrorException, IncalculableExpressionException, IllegalExpressionException, UnknownOperatorException {
        AbnormalRule result = abnormalRuleService.create(abnormalRule);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("根据条件查询异常规则")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationIds", value = "电站ID(可传多个，用逗号分隔)", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "objectId", value = "对象ID（可传电站，能量单元，设备ID）", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "queryStr", value = "查询条件", dataType = "string", paramType = "query")
    })
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<ResultObject> search(@RequestParam("stationIds") Optional<List<Long>> stationIds,
                                               @RequestParam("objectId") Optional<Long> objectId,
                                               @RequestParam("queryStr") Optional<String> queryStr,
                                               @ModelAttribute PageParam pageParam) {
        Iterable<AbnormalRule> result = abnormalRuleService.search(stationIds, objectId, queryStr, pageParam);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    /**
     * todo 现阶段每个异常规则只有一个异常公式，以后删除
     * @param ids
     * @param alarmLevelId
     * @return
     * @throws ObjectNotFoundException
     * @throws UniqueConstraintsException
     * @throws ArgumentErrorException
     */
    @ApiOperation("根据异常规则ID和异常等级ID修改匹配等级（过渡使用）")
    @RequestMapping(value = "/updateLevel", method = RequestMethod.PATCH)
    public ResponseEntity<ResultObject> update(@RequestParam("ids") List<Long> ids,
                                               @RequestParam("alarmLevelId") Long alarmLevelId) throws ObjectNotFoundException, UniqueConstraintsException, ArgumentErrorException {
        abnormalRuleService.updateLevel(ids, alarmLevelId);
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }

    @ApiOperation("全量更新异常规则")
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public ResponseEntity<ResultObject> update(@RequestBody AbnormalRule abnormalRule) throws IncalculableExpressionException, UnknownOperatorException, IllegalExpressionException, ArgumentErrorException {
        AbnormalRule result = abnormalRuleService.update(abnormalRule);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("根据异常代码Id获取异常规则代码")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> getAlarmLevelCodeById(@PathVariable("id") Long id) {
        AbnormalRule result = abnormalRuleService.findById(id);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("根据ID删除告警等级")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "告警状态ID", required = true, dataType = "long", paramType = "path")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<ResultObject> delete(@PathVariable("id") Long id) {
        abnormalRuleService.delete(id);
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }

    @ApiOperation("删除告警规则id和对象的映射map缓存")
    @RequestMapping(value = "/ruleDtoMap", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> getRuleDtoMap() {
        Map<String, AbnormalRuleDTO> result = abnormalRuleService.getRuleDtoMap();
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("删除告警规则id和对象的映射map缓存")
    @RequestMapping(value = "/ruleDtoMap", method = RequestMethod.DELETE)
    public ResponseEntity<ResultObject> deleteRuleDtoMap() {
        abnormalRuleService.deleteRuleDtoMap();
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }
}
